<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link href="../../component/pear/css/pear.css" rel="stylesheet"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">关键字</label>
                <div class="layui-input-inline">
                    <input type="text" name="keyWord" placeholder="规格名称" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="specs-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="table" lay-filter="specs-filter"></table>
    </div>
</div>

</body>

<script type="text/html" id="record-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
        <i class="layui-icon layui-icon-edit"></i>
    </button>
</script>

<script type="text/html" id="table-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>

<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'common', 'easyAdmin'], function () {
        let easyAdmin = layui.easyAdmin;
        let MODULE_PATH = "specs";
        let table = layui.table;
        let form = layui.form;
        let common = layui.common;
        let $ = layui.jquery;

        let cols = [
                [{
                    type: 'checkbox'
                },
                    {
                        title: '规格序号',
                        field: 'commSpecsId',
                        align: 'center'
                    },
                    {
                        title: '规格名称',
                        field: 'commSpecsName',
                        align: 'center'
                    },
                    {
                        title: '创建时间',
                        field: 'createdTime',
                        align: 'center'
                    },
                    {
                        title: '操作',
                        toolbar: '#record-toolbar',
                        align: 'center'
                    }
                ]
            ]
        ;

        easyAdmin.tableRender({
            elem: '#table', // 对应table的id
            url: '/comm/specs',
            page: true, // 分页参数可以自定义
            cols: cols, // 列表示
            skin: 'line', // 表格样式
            toolbar: '#table-toolbar', // 表格头部工具栏 可定义左上角和右上角工具栏,一般用于左上角配置
            defaultToolbar: [{  // 自由配置头部工具栏右侧的图标按钮
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        // toolbar: '#toolbarDemo'
        // toolbar 点击头部工具栏区域设定了属性为 lay-event="" 的元素时触发(包括左上角和右上角)
        table.on('toolbar(specs-filter)', function (obj) {
            switch (obj.event) {
                case 'add':
                    SpecsAdd(MODULE_PATH);
                    break;
                case 'refresh':
                    easyAdmin.tableRefresh('table');
                    break;
            }
        });

        table.on('tool(specs-filter)', function (obj) {
            switch (obj.event) {
                case 'edit':
                    SpecsEdit(MODULE_PATH, obj.data.commSpecsId);
                    break;
            }
        });

        /**
         * 跳转到add页面
         */
        let SpecsAdd = function (MODULE_PATH) {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: [common.isModile() ? '100%' : '500px', common.isModile() ? '100%' : '300px'],
                content: MODULE_PATH + '/add.html'
            });
        }

        /**
         * 跳转到edit页面
         */
        let SpecsEdit = function (MODULE_PATH, id) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['500px', '300px'],
                content: [MODULE_PATH + '/edit.html?id=' + id, 'no']
            });
        }

        // 触发submit提交 按钮点击或者表单被执行提交时触发
        form.on('submit(specs-query)', function (data) {
            table.reload('table', {
                // //设定异步数据接口的额外参数
                where: data.field
            })
            return false; // 阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    })
</script>
</html>
